package com.huixuebao.after.controller.merchants;


import cn.hutool.core.util.IdUtil;
import com.huixuebao.after.dto.BaseDivisionDTO;
import com.huixuebao.after.dto.ImagePathDTO;
import com.huixuebao.after.dto.MerchantsDTO;
import com.huixuebao.after.service.BaseDivisionService;
import com.huixuebao.after.service.MachanismService;
import com.huixuebao.after.util.BizLicenseOCR;
import com.huixuebao.after.util.MFileUtil;
import com.huixuebao.after.util.OSSClientUtil;
import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/hxb/after/merchants")
public class merchantsController {

    @Autowired
    private MachanismService machanismService;

    @Autowired
    private BaseDivisionService baseDivisionService;

    /**
     * 获取机构列表信息
     * @param courrent
     * @param size
     * @param cityIds
     * @param merchantsName
     * @param state
     * @return
     */
    @GetMapping("/getMerchantsList")
    public ApiResult getMerchantsList(Integer courrent,
                                      Integer size,
                                      String cityIds,
                                      String merchantsName,
                                      String state,
                                      String contactsName,
                                      String contactsPhone,
                                      HttpServletRequest request){
        return machanismService.getMerchantsList(courrent,size,cityIds,merchantsName,state,contactsName,contactsPhone,request);
    }

    /**
     * 添加机构
     * @return
     */
    @PostMapping("/addMerchants")
    public ApiResult addMerchants(@RequestBody MerchantsDTO merchantsDTO,HttpServletRequest request){
        return machanismService.addMerchants(merchantsDTO,request);
    }

    /**
     * 获取区码表信息树结构
     * @return
     */
    @PostMapping("/getAreaTree")
    public ApiResult<List<BaseDivisionDTO>> getAreaTree(){
        return machanismService.getAreaTree();
    }

    /**
     * 获取区码列表()
     * 获取区码字段数据
     */
    @GetMapping("/getAreaCode")
    public ApiResult getAreaCode(){
        return baseDivisionService.getAreaCode();
    }

    /**
     * 获取营业执照识别信息
     */
    @PostMapping("/getLicenseOCR")
    public ApiResult getLicenseOCR(@RequestParam("file") MultipartFile file){
        ApiResult apiResult = new ApiResult();
        ImagePathDTO imagePathDTO = new ImagePathDTO();
        try {
            String imageFile = file.getOriginalFilename();
            File tempFile = File.createTempFile(IdUtil.simpleUUID(),"." + MFileUtil.getExp(imageFile));
            file.transferTo(tempFile);
            String url = OSSClientUtil.uploadObject2OSS(tempFile);
            String bizLicenseOCRInfo = BizLicenseOCR.getLicenseOCR(url);
            if (StringUtils.isBlank(bizLicenseOCRInfo)){
                apiResult.setSuccess(false);
                apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
                apiResult.setMessage("获取营业执照信息失败");
            }else {
                apiResult.setSuccess(true);
                apiResult.setData(bizLicenseOCRInfo);
                apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                apiResult.setMessage("获取营业执照信息成功");
            }
            imagePathDTO.setImagePath(url);
            imagePathDTO.setData(bizLicenseOCRInfo);
            apiResult.setData(imagePathDTO);
            apiResult.setSuccess(true);
            apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
            apiResult.setMessage("保存.Jpg成功！！");
        }catch (Exception e){
            log.info("保存图片异常");
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("保存.Jpg失败！！");
        }
        return apiResult;
    }

    /**
     * 编辑机构信息
     */
    @PostMapping("/editMerchants")
    public ApiResult editMerchants(@RequestBody MerchantsDTO merchantsDTO){

        return machanismService.editMerchants(merchantsDTO);
    }

    /**
     * 商户信息(查看商户自己到信息)
     * @return
     */
    @GetMapping("/getMerchantsNews")
    public ApiResult getMerchantsNews(HttpServletRequest request){
        return machanismService.getMerchantsNews(request);
    }
    /**
     * 获取子集商户列表(自己查看自己的子集商户)
     */
    @GetMapping("/getChildrenMerchant")
    public ApiResult getChildrenMerchant(HttpServletRequest request){
        return machanismService.getChildrenMerchant(request);
    }
}
